﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WXYW.BLL;
using WXYW.DAL;

namespace WXYW
{
    public partial class EditProductType : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindProductTypeInfo();
            }
        }

        private void BindProductTypeInfo()
        {
            int productTypeID = Convert.ToInt32(Request.QueryString[Constants.QUERYSTR_PRODUCTTYPE_ID]);
            ProductClassService service = new ProductClassService();
            ProductType productType = service.GetProductType(productTypeID);
            productTypeTxtBox.Text = productType.ProductTypeName;

            productClassDDL.DataSource = service.GetAllClass();
            productClassDDL.DataTextField = "Name";
            productClassDDL.DataValueField = "ID";
            productClassDDL.SelectedValue = productType.ProductClass.ID.ToString();
            productClassDDL.DataBind();

            backLinkButton.PostBackUrl = string.Format("ManageProductType.aspx?{0}={1}", Constants.QUERYSTR_PAGE_NO, Request.QueryString[Constants.QUERYSTR_PAGE_NO]);
        }

        protected void okLinkButton_Click(object sender, EventArgs e)
        {
            int productTypeID = Convert.ToInt32(Request.QueryString[Constants.QUERYSTR_PRODUCTTYPE_ID]);
            ProductClassService service = new ProductClassService();
            ProductType productType = service.GetProductType(productTypeID);
            productType.ProductClass = service.GetProductClass(Convert.ToInt32(productClassDDL.SelectedValue));
            productType.ProductTypeName = productTypeTxtBox.Text.Trim();
            service.UpdateProductType(productType);
            Response.Write(string.Format("<script language='JavaScript'> alert( '信息保存成功！');location.href= 'ManageProductType.aspx?{0}={1}' </script> ", Constants.QUERYSTR_PAGE_NO, Request.QueryString[Constants.QUERYSTR_PAGE_NO])); 
        }
    }
}
